package vn.edu.uit.chemistry;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import vn.edu.uit.chemistry.utilities.TCPClient;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class NopBaiCu extends Activity{
	
	TextView tvFile;
	TextView tvSize;
	TextView tvReceiver;
	
	ImageView imgGuiFile;
	String ptString = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_nop_bai_cu);
		
		Intent startIntent = getIntent();
		if (startIntent != null) {
			Bundle b = startIntent.getExtras();
			ptString = b.getString("File");
		}
		
		SharedPreferences prefs = getSharedPreferences("TaiKhoanGV", 0);
		String hotenGV = prefs.getString("hoten", "").toString();
		
		File f = new File(ptString);
		
		tvFile = (TextView) findViewById(R.id.tvFile);
		tvFile.setText(f.getName());
		
		tvSize = (TextView) findViewById(R.id.tvSize);
		tvSize.setText(""+f.length()+" bytes");
		
		tvReceiver = (TextView) findViewById(R.id.tvReceiver);
		tvReceiver.setText(hotenGV);
		
		imgGuiFile = (ImageView) findViewById(R.id.imgGuiFile);
		imgGuiFile.postDelayed(onEverySecond, 0);
		
		new SendBaiCuAsyncTask().execute();
	}
	
	final class SendBaiCuAsyncTask extends AsyncTask<Void, Void, Void> {

		Context ctx;
		
		public SendBaiCuAsyncTask() {
			this.ctx = NopBaiCu.this;
		}
		
		@Override
		protected Void doInBackground(Void... arg0) {
			SharedPreferences prefs = getSharedPreferences("TaiKhoanGV", 0);
			String ipGV = prefs.getString("ip", "").toString();
			String portGV = prefs.getString("port", "").toString();

			try {
				TCPClient client = new TCPClient(ctx, ipGV, Integer.parseInt(portGV));
				client.nopBaiCu(ptString);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			return null;
		}
		
	}
	
	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new SendAsyncTask().execute();
		}
	};
	
	final class SendAsyncTask extends AsyncTask<Void, Void, Void> {
		
		private int finish = 0;
		
		public SendAsyncTask() {
			
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			SharedPreferences ref = getSharedPreferences("GuiFileGV", 0);
			finish = ref.getInt("finish", 0);
			return null;
		}
		
		@Override
		protected void onPostExecute(final Void unused) {
			if (finish != 0) {
				imgGuiFile.removeCallbacks(onEverySecond);
				SharedPreferences ref = getSharedPreferences("GuiFileGV", 0);
				SharedPreferences.Editor editor = ref.edit();
				editor.putInt("finish", 0);
				editor.commit();
				finish();
			} else {
				imgGuiFile.postDelayed(onEverySecond, 0);
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return false;
	}
}
